package com.mytijian.openapi.adapter.dianping.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mytijian.openapi.adapter.dianping.dao.AsyncRequestDAO;
import com.mytijian.openapi.adapter.dianping.dao.dataobject.AsyncRequestDO;
import com.mytijian.openapi.adapter.dianping.dao.param.AsyncRequestDAOQuery;
import com.mytijian.openapi.adapter.dianping.model.AsyncRequest;
import com.mytijian.openapi.adapter.dianping.param.AsyncRequestQuery;
import com.mytijian.openapi.adapter.dianping.service.AsyncRequestService;

@Service
public class AsyncRequestServiceImpl implements AsyncRequestService {

	@Autowired
	private AsyncRequestDAO asyncRequestDAO;

	@Override
	public boolean saveRequest(AsyncRequest request) {
		if (request!=null) {
			AsyncRequestDO requestDO = new AsyncRequestDO();
			BeanUtils.copyProperties(request, requestDO);
			return 1 == asyncRequestDAO.insert(requestDO);
		}
		return false;
	}

	@Override
	public List<AsyncRequest> listRequests(AsyncRequestQuery query) {
		if (query != null) {
			AsyncRequestDAOQuery daoQuery = new AsyncRequestDAOQuery();
			BeanUtils.copyProperties(query, daoQuery);

			List<AsyncRequestDO> requestDoList = asyncRequestDAO.listRequests(daoQuery);

			List<AsyncRequest> result = new ArrayList<>();

			for (AsyncRequestDO d : requestDoList) {
				AsyncRequest request = new AsyncRequest();
				BeanUtils.copyProperties(d, request);
				result.add(request);
			}
			return result;
		}

		return null;
	}

	@Override
	public boolean updateRequest(AsyncRequest request) {
		if (request!=null) {
			AsyncRequestDO requestDO = new AsyncRequestDO();
			BeanUtils.copyProperties(request, requestDO);
			return 1 == asyncRequestDAO.update(requestDO);
		}
		return false;
	}

}
